package com.yifeng.repo.flink.data.transport.config;

import java.io.Serializable;
import java.time.Duration;
import java.util.*;

/**
 * 数据库连接的数据源字段
 * @author lijing
 * @since 2023-01-04
 */
public class DataSourceConfig implements Serializable,Cloneable {
	private static final long serialVersionUID = 1L;
	private static final Integer MULTI_TABLE_DEFAULT_PARALLELISM = 5;

    private String dbType;
    private String hostname;
    private int port;
    private String jdbcUrl;
    private String username;
    private String password;
    private String database;
    private String tableName;
    private String serverTimeZone;
    private String snapshotMode;
    private int fetchSize;
    private int connectMaxRetries;
    private int connectionPoolSize;
    private Duration connectTimeout;
    private String driverClassName;
    private Integer parallelism;
    //schema列表
    private List<String> schemaList = new ArrayList<String>();
    //表名列表
    private List<String> tableList = new ArrayList<String>();
    //字段名
    private Map<String,List<Object>> tableFieldMap = new HashMap<String,List<Object>>();
    //直接sql语句
    private Map<String,String> sqlCondition = new HashMap<String,String>(); 
    
	public String getJdbcUrl() {
        return jdbcUrl;
    }

    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDbType() {
		return dbType;
	}

	public void setDbType(String dbType) {
		this.dbType = dbType;
	}

	public String getHostname() {
		return hostname;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public String getServerTimeZone() {
		return serverTimeZone;
	}

	public void setServerTimeZone(String serverTimeZone) {
		this.serverTimeZone = serverTimeZone;
	}

	public Duration getConnectTimeout() {
		return connectTimeout;
	}

	public void setConnectTimeout(Duration connectTimeout) {
		this.connectTimeout = connectTimeout;
	}

	public String getSnapshotMode() {
		return snapshotMode;
	}

	public void setSnapshotMode(String snapshotMode) {
		this.snapshotMode = snapshotMode;
	}

	public int getFetchSize() {
		return fetchSize;
	}

	public void setFetchSize(int fetchSize) {
		this.fetchSize = fetchSize;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public int getConnectMaxRetries() {
		return connectMaxRetries;
	}

	public void setConnectMaxRetries(int connectMaxRetries) {
		this.connectMaxRetries = connectMaxRetries;
	}
	
	public int getConnectionPoolSize() {
		return connectionPoolSize;
	}

	public void setConnectionPoolSize(int connectionPoolSize) {
		this.connectionPoolSize = connectionPoolSize;
	}
	
	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}
	
	public List<String> getSchemaList() {
		return schemaList;
	}

	public void setSchemaList(List<String> schemaList) {
		this.schemaList = schemaList;
	}

	public List<String> getTableList() {
		return tableList;
	}

	public void setTableList(List<String> tableList) {
		this.tableList = tableList;
	}
	
	public Map<String, List<Object>> getTableFieldMap() {
		return tableFieldMap;
	}

	public void setTableFieldMap(Map<String, List<Object>> tableFieldMap) {
		this.tableFieldMap = tableFieldMap;
	}
	
	public Map<String, String> getSqlCondition() {
		return sqlCondition;
	}

	public void setSqlCondition(Map<String, String> sqlCondition) {
		this.sqlCondition = sqlCondition;
	}
	
	public int getParallelism() {
		if(Objects.isNull(parallelism)) {
			parallelism = MULTI_TABLE_DEFAULT_PARALLELISM;
		}
		if(parallelism > getTableList().size()) {
			return getTableList().size();
		}
		return parallelism;
	}

	public void setParallelism(Integer parallelism) {
		this.parallelism = parallelism;
	}

	@Override
    public String toString() {
        return "DataSourceConfig{" +
            ", username='" + username + '\'' +
            ", tableName='" + tableName + '\'' +
            ", tableName='" + tableName + '\'' +
            '}';
    }
}
